package com.dev;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import com.log;
import com.servlet.GUIHttpHost;
import android.content.res.AssetManager;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.File;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView.OnEditorActionListener;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
import android.util.TypedValue;
import android.graphics.Color;
public class ActionMenu extends LinearLayout implements OnClickListener{
    WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	GUI.Item itSettings;
	GUI.Item itGetStart;
	GUI.Item itClose;
	GUI.Item itCreateImage;
	GUI.Item itSVGImage;
	GUI.Item itRemote;
	GUI.Item itCollect;
    GUI.Item itFont;
	MainActivity ma;
	ActionMenu(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setBackgroundDrawable(new GUI.BorderDrawable());
		LayoutParams itlp=new LayoutParams(-1,(int)(30*ma.dp));
		wlp.gravity=Gravity.LEFT|Gravity.TOP;
		wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
		itSettings=new GUI.Item(ma);
		itSettings.setText("设置");
		itSettings.setOnClickListener(this);
		itGetStart=new GUI.Item(ma);
		itGetStart.setText("开始");
		itGetStart.setOnClickListener(this);
		itClose=new GUI.Item(ma);
		itClose.setText("取消");
		itClose.setOnClickListener(this);
		itCreateImage=new GUI.Item(ma);
		itCreateImage.setText("新建栅格图像");
		itCreateImage.setOnClickListener(this);
		itRemote=new GUI.Item(ma);
		itRemote.setText("servlet");
		itRemote.setOnClickListener(this);
		itCollect=new GUI.Item(ma);
		itCollect.setText("我的收藏");
		itCollect.setOnClickListener(this);
		itSVGImage=new GUI.Item(ma);
		itSVGImage.setText("新建矢量图像");
		itSVGImage.setOnClickListener(this);
        itFont=new GUI.Item(ma);
        itFont.setText("新建ttf");
        itFont.setOnClickListener(this);
		addView(itSettings,itlp);
		addView(itGetStart,itlp);
		addView(itRemote,itlp);
		addView(itCreateImage,itlp);
		addView(itSVGImage,itlp);
        addView(itFont,itlp);
		addView(itCollect,itlp);
		addView(itClose,itlp);
	}
	void show(){
		wlp.width=ma.wp/2;
		wlp.height=-2;
		Rect rect=new Rect();
		ma.gui.ivMenu.getGlobalVisibleRect(rect);
		wlp.x=rect.left;
		wlp.y=rect.bottom;
		ma.wm.addView(this,wlp);
	}
	void dismiss(){
		ma.wm.removeView(this);
	}
	@Override
	public void onClick(View v) {
		if(v==itClose){
			dismiss();
		}else if(v==itSettings){
			if(ma.settings==null)ma.settings=new GUISettings(ma);
			ma.setContentView(ma.settings);
			dismiss();
		}else if(v==itGetStart){
			ma.gui.TabOnClick.onClick(null);
			dismiss();
		}else if(v==itCreateImage){
			try{
			if(QueryNewImage.qni==null)QueryNewImage.qni=new QueryNewImage(ma);
			QueryNewImage.qni.show();
			dismiss();
			}catch(Exception e){log.alert(e.toString());}
		}else if(v==itCollect){
			if(ma.collections==null)ma.collections=new GUICollections(ma);
			ma.setContentView(ma.collections);
            dismiss();
		}else if(v==itRemote){
			if(ma.httpHost==null)ma.httpHost=new GUIHttpHost(ma);
			ma.setContentView(ma.httpHost);
            dismiss();
		}else if(v==itFont){
            if(ma.gui.qcf==null)ma.gui.qcf=new QueryCreateFont(ma);
            ma.gui.qcf.show(null);
        }
	}
	Rect visibile=new Rect();
	@Override
	protected void onLayout(boolean changed,int l,int t,int r,int b){
		super.onLayout(changed,l,t,r,b);
		getGlobalVisibleRect(visibile);
	}
	@Override
	public boolean onTouchEvent(MotionEvent event){
		int rx=(int)event.getX();
		int ry=(int)event.getY();
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:
				if(rx<visibile.left||rx>=visibile.right||ry<visibile.top||ry>=visibile.bottom){
					dismiss();
				}
		}
		return super.onTouchEvent(event);
	}
    public static class QueryCreateFont extends LinearLayout implements OnClickListener,OnEditorActionListener{
        WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
        MainActivity ma;
        UnderlineSingleLineEditor editor1;
        UnderlineSingleLineEditor editor2;
        LinearLayout titlebar;
        LinearLayout line1,line2;
        IconViewClose ivc;
        ShowSoftInput ssi;
        TextView title,hint1,hint2;
        TextView create;
        File dir;
        QueryCreateFont(MainActivity ma){
            super(ma);
            this.ma=ma;
            LayoutParams flexlp=new LayoutParams(0,-1,1f);
            setBackground(new GUI.BorderDrawable());
            setOrientation(1);
            wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
            wlp.gravity=Gravity.CENTER;
            create=new TextView(ma);
            create.setGravity(Gravity.CENTER);
            create.setText("创建");
            create.setTextSize(16);
            create.setTextColor(0xff00bbff);
            create.setOnClickListener(this);
            editor1=new UnderlineSingleLineEditor(ma);
            editor1.setOnEditorActionListener(this);
            editor1.setPadding(0,0,9,0);
            editor1.setHint("文件名");
            editor2=new UnderlineSingleLineEditor(ma);
            editor2.setOnEditorActionListener(this);
            editor2.setPadding(0,0,9,0);
            editor2.setHint("字体名");
            hint1=new TextView(ma);
            hint1.setPadding(9,0,0,0);
            hint1.setText("文件名：");
            hint1.setTextSize(13);
            hint2=new TextView(ma);
            hint2.setPadding(9,0,0,0);
            hint2.setText("字体名：");
            hint2.setTextSize(13);
            line1=new LinearLayout(ma);
            line1.setGravity(Gravity.CENTER_VERTICAL);
            line1.setOrientation(0);
            line1.setPadding(9,0,9,0);
            line1.addView(hint1,-2,-1);
            line1.addView(editor1,flexlp);
            line2=new LinearLayout(ma);
            line2.setGravity(Gravity.CENTER_VERTICAL);
            line2.setOrientation(0);
            line2.setPadding(9,0,9,0);
            line2.addView(hint2,-2,-1);
            line2.addView(editor2,flexlp);
            ivc=new IconViewClose(ma);
            ivc.setOnClickListener(this);
            ssi=new ShowSoftInput(ma);
            ssi.setOnClickListener(this);
            title=new TextView(ma);
            title.setText("新建ttf");
            title.setTextSize(14);
            title.setTextColor(0x7f7f7f7f);
            titlebar=new LinearLayout(ma);
            titlebar.setOrientation(HORIZONTAL);
            titlebar.setBackground(new GUI.BorderDrawable());
            titlebar.addView(title,flexlp);
            titlebar.addView(ssi);
            titlebar.addView(ivc);
            addView(titlebar,-1,ma.dialogTitleBarHeight);
            addView(line1,-1,ma.dialogRowHeight);
            addView(line2,-1,ma.dialogRowHeight);
            addView(create,-1,ma.dialogRowHeight);
        }
        void show(File dir){
            if(dir==null)dir=ma.workDir;
            this.dir=dir;
            editor1.setText("sample.ttf");
            editor2.setText("sample");
            wlp.width=ma.wp*2/3;
            wlp.height=-2;
            ma.wm.addView(this,wlp);
        }
        @Override
        public void onClick(View v) {
            if(v==ivc){
                ma.wm.removeView(this);
            }else if(v==ssi){
                if(editor1.hasFocus()){
                    ma.imm.showSoftInput(editor1,0);
                    String s=editor1.getEditableText().toString();
                    int dot=s.lastIndexOf('.');
                    if(dot>0){
                        editor1.setSelection(0,dot);
                    }else{
                        editor1.selectAll();
                    }
                }else if(editor2.hasFocus()){
                    ma.imm.showSoftInput(editor2,0);
                    String s=editor2.getEditableText().toString();
                    int dot=s.lastIndexOf('.');
                    if(dot>0){
                        editor2.setSelection(0,dot);
                    }else{
                        editor2.selectAll();
                    }
                }else if(editor1.requestFocus()){
                    ma.imm.showSoftInput(editor1,0);
                    String s=editor1.getEditableText().toString();
                    int dot=s.lastIndexOf('.');
                    if(dot>0){
                        editor1.setSelection(0,dot);
                    }else{
                        editor1.selectAll();
                    }
                }
            }else if(v==create){
                String filename=editor1.getEditableText().toString();
                String fontname=editor2.getEditableText().toString();
                if(filename.isEmpty()){
                    Toast.makeText(ma,"文件名不能为空",0).show();
                }else if(filename.contains("/")){
                    Toast.makeText(ma,"文件名不能包含/",0).show();
                }
                if(fontname.isEmpty()){
                    Toast.makeText(ma,"字体名不能为空",0).show();
                }
                
                AssetManager am=ma.getAssets();
                try{
                    File dst=new File(dir,filename);
                    if(dst.createNewFile()){
                        InputStream is=am.open("DroidSansMono.ttf");
                        FileOutputStream fos=new FileOutputStream(dst,false);
                        
                        byte[] buffer=new byte[1024];
                        int len=is.read(buffer);
                        while(len>0){
                            fos.write(buffer,0,len);
                            len=is.read(buffer);
                        }
                        fos.close();
                        is.close();
                        Toast.makeText(ma,"创建成功！",0).show();
                        ma.wm.removeView(this);
                    }else{
                        Toast.makeText(ma,"创建文件失败",0).show();
                    }
                    
                }catch(Exception e){
                    log.alert(e.toString());
                }
            }
        }

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            return false;
        }
    }
}
